package core

import (
	"animal/octopus/common"
	"animal/octopus/orm"
	"strings"

	log "github.com/sirupsen/logrus"
	"gorm.io/gorm"
)

// ------------------------------------------------------------------------------
//
// ------------------------------------------------------------------------------
func GetReportRuleList(moduleID uint) ([]common.ReportRuleInfo, error) {
	return orm.GetReportRuleList(nil, moduleID)
}

// ------------------------------------------------------------------------------
//
// ------------------------------------------------------------------------------
func CreateReportRule(param common.CreateReportRuleParam) (uint, error) {
	if param.MatchType != "type" && param.MatchType != "name" {
		log.WithField("matchType", param.MatchType).Error("invalid match type")
		return 0, common.NewErrCode(common.ErrInvalidParam)
	}

	if strings.TrimSpace(param.MatchRule) == "" {
		log.WithField("matchRule", param.MatchRule).Error("invalid match rule")
		return 0, common.NewErrCode(common.ErrInvalidParam)
	}

	if strings.TrimSpace(param.Parent) == "" {
		log.WithField("parent", param.Parent).Error("invalid parent")
		return 0, common.NewErrCode(common.ErrInvalidParam)
	}

	return orm.CreateReportRule(nil, param)
}

// ------------------------------------------------------------------------------
//
// ------------------------------------------------------------------------------
func EditReportRule(param common.EditReportRuleParam) error {
	if param.MatchType != "type" && param.MatchType != "name" {
		log.WithField("matchType", param.MatchType).Error("invalid match type")
		return common.NewErrCode(common.ErrInvalidParam)
	}

	if strings.TrimSpace(param.MatchRule) == "" {
		log.WithField("matchRule", param.MatchRule).Error("invalid match rule")
		return common.NewErrCode(common.ErrInvalidParam)
	}

	if strings.TrimSpace(param.Parent) == "" {
		log.WithField("parent", param.Parent).Error("invalid parent")
		return common.NewErrCode(common.ErrInvalidParam)
	}

	return orm.EditReportRule(nil, param)
}

// ------------------------------------------------------------------------------
//
// ------------------------------------------------------------------------------
func DeleteReportRule(param common.DeleteReportRuleParam) error {
	return orm.DeleteReportRule(nil, param)
}

// ------------------------------------------------------------------------------
//
// ------------------------------------------------------------------------------
func ImportReportRules(moduleID uint, params []common.ImportReportRuleParam) error {
	if err := common.DB(nil).Transaction(func(tx *gorm.DB) error {
		for _, param := range params {
			if _, err := orm.CreateReportRule(tx, common.CreateReportRuleParam{
				ModuleID:  moduleID,
				Desc:      param.Desc,
				MatchType: param.MatchType,
				MatchRule: param.MatchRule,
				Parent:    param.Parent,
			}); err != nil {
				log.WithFields(log.Fields{
					"module": moduleID,
					"param":  common.MustMarshal(param),
				}).Error("create report rule failed")
				return err
			}
		}
		return nil
	}); err != nil {
		log.WithField("error", err).Error("import report rules failed")
		return err
	}

	log.WithField("module", moduleID).Info("import report rules success")

	return nil
}
